#!/usr/bin/env python3
"""
基础Web测试 - 确保Flask可以正常运行
"""

from flask import Flask
import socket

app = Flask(__name__)

@app.route('/')
def hello():
    return '''
    <html>
    <head>
        <title>基础测试</title>
        <style>
            body { 
                font-family: Arial; 
                text-align: center; 
                padding: 50px;
                background: #f0f8ff;
            }
            .container { 
                background: white; 
                padding: 30px; 
                border-radius: 10px; 
                box-shadow: 0 4px 20px rgba(0,0,0,0.1);
                max-width: 600px;
                margin: 0 auto;
            }
            h1 { color: #4CAF50; }
            .success { color: #4CAF50; font-weight: bold; }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>🌱 智慧农业系统基础测试</h1>
            <div class="success">✅ Flask服务器运行正常</div>
            <p>如果您能看到这个页面，说明Web服务可以正常访问。</p>
            <p>现在可以启动完整的智慧农业系统了。</p>
            <hr>
            <p><strong>下一步：</strong></p>
            <p>在终端中运行：<code>python3 智慧农业Web演示系统.py</code></p>
        </div>
    </body>
    </html>
    '''

@app.route('/test')
def test():
    return {'status': 'ok', 'message': 'Web服务正常'}

def find_port():
    """找到可用端口"""
    for port in range(8080, 8100):
        try:
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                s.bind(('', port))
                return port
        except OSError:
            continue
    return 8080

if __name__ == '__main__':
    port = find_port()
    print(f"🌐 基础测试服务启动在端口: {port}")
    print(f"🔗 请访问: http://localhost:{port}")
    print(f"🧪 API测试: http://localhost:{port}/test")
    
    try:
        app.run(host='0.0.0.0', port=port, debug=False)
    except Exception as e:
        print(f"启动失败: {e}")